package com.mozz.finally_work.dao;

import com.mozz.finally_work.pojo.User;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

public interface SubscribeDao {

    /*关注用户*/
    @Insert("INSERT INTO subscribe (id,user_id,target_user_id) values(#{id},#{userId},#{targetId})")
    Integer subscribeUser(@Param("id") String id, @Param("userId") String userId, @Param("targetId") String targetId);

    /* 取消关注*/
    @Delete("DELETE FROM subscribe where user_id=#{userId} and target_user_id=#{targetId}")
    Integer unSubscribeUser(@Param("userId") String userId, @Param("targetId") String targetId);

    /*根据用户id查询关注数*/
    @Select("SELECT count(*) as subscribeCount from subscribe WHERE user_id=#{userId}")
    Integer countUserSubscribe(String userId);

    /*查看是否已关注*/
    @Select("SELECT count(*) FROM subscribe where user_id=#{userId} and target_user_id=#{targetId}")
    Integer isSubscribe(@Param("userId") String userId,@Param("targetId") String targetId);

    /*获取关注列表*/
    @Select("SELECT * from user  where id in  (SELECT target_user_id from subscribe where user_id=#{userId})")
    @Results(value = {
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "nick_name", property = "nickName"),
            @Result(property = "subscribeCount", column = "id", one = @One(select = "com.mozz.finally_work.dao.SubscribeDao.countUserSubscribe", fetchType = FetchType.EAGER)),
            @Result(property = "blogCount", column = "id", one = @One(select = "com.mozz.finally_work.dao.BlogDao.countBlog", fetchType = FetchType.EAGER)),
            @Result(property = "collectionCount", column = "id", one = @One(select = "com.mozz.finally_work.dao.CollectionDao.countCollection", fetchType = FetchType.EAGER)),
    })
    List<User> querySubscribeUsers(String userId);
}
